from concurrent.futures import thread
import numpy as np
import pymc3 as pm
import theano.tensor as tt
import scipy.stats as stats
from sklearn.metrics import confusion_matrix,roc_auc_score,roc_curve
from simulation_data import *
from mcmc import mcmc
from indicator import get_specific_sensitive,p_to_flag_pre
from multi_thread import MulThread
from tqdm import tqdm
from save import save
import gc 

def main(n,i,rt,flag,):
    # 参数估计
    trace = mcmc(n,i,rt)
    alphas = [0.2,0.05,0.01]
    indicatiors=[ get_specific_sensitive(
                    confusion_matrix(
                        flag,p_to_flag_pre(trace.posterior["l_0"].mean(axis=0).mean(axis=0),alpha),normalize="true",labels=[1,0]
                                    )
                                        ) 
                for alpha in alphas]
    return np.array(indicatiors)

def run():
    c1 = []
    for (n,) in zip([500,1000]):
        c2 = []
        for (i,) in zip([10,20]):
            c3 = []
            for (ab_p,) in zip([0.05,0.1,0.2]):
                simu_data = simulation_data(n,i,ab_p)
                indicatior_l = []
                for c,(t,) in enumerate(zip(simu_data[1])):# 取出三次异常作答的rt\
                    indicatior = main(n,i,t,simu_data[2][c])
                    indicatior_l.append(indicatior)
                    gc.collect()
                c3.append(indicatior_l)
            c2.append(c3)
        c1.append(c2)
    save(c1)
    
    

run()
# bar = tqdm(desc='总进度:',total=15)
# import time
# for r in range(5):
#     threads = []
#     for t in range(3):
#         threads.append(MulThread(run))

#     for t in threads:
#         t.start()
#         time.sleep(10)

#     for t in threads:
#         t.join()
#         bar.update(1)
#         time.sleep(10)
